import { defineStore } from 'pinia'
import cache from '../utils/cache'
import { ILoginData, IUserVO } from '../api/user/type'
import { managePlugin } from '../plugins/managePlugin'

export const appStore = defineStore('app', {
  state: () => ({
    loginForm: {
      name: '', //姓名
      mobile: '', //手机号
      type: 0, //1-员工，2-非员工
      jobNumber: '' //工号
    } as ILoginData,
    loginUser: {
      loginId: '',
      password: '',
      proxyIp: '',
      proxyPort: '',
      tenantId: ''
    } as IUserVO,
    installStepIndex: 0, //安装步骤编号
    token: cache.token.get(),
    phoneBrand: cache.phoneBrand.get(), //手机型号
    serverPath: cache.serverPath.get(),
    managerName: cache.managerName.get(),
    managerPassword: cache.managerPassword.get(),
    language: cache.language.get() || 'cn',
    toolsName:  ''
  }),
  actions: {
    setToolsName(val) {
      this.toolsName = val
    },
    setLanguage(val) {
      this.language = val
      cache.language.set(val)
    },
    setLoginForm(val: any) {
      this.loginForm = val || {
        name: '',
        mobile: '', //手机号
        type: 0, //1-员工，2-非员工
        jobNumber: '' //工号
      }
    },
    setLoginUser(val) {
      this.loginUser = val
    },
    setToken(val: any) {
      this.token = val
      cache.token.set(val)
    },
    setPhoneBrand(val: any) {
      this.phoneBrand = val
      cache.phoneBrand.set(val)
    },
    setInstallStepIndex(val: any) {
      this.installStepIndex = val
    },
    async setServerPath(path, localSave = true) {
      this.serverPath = path
      cache.serverPath.set(path)
      if (localSave) await zipPlugin.setLocalItemPreload('serverPath', path)
    },
    async setManagerName(managerName, localSave = true) {
      this.managerName = managerName
      cache.managerName.set(managerName)
      if (localSave) await zipPlugin.setLocalItemPreload('managerName', managerName)
    },
    async setManagerPassword(managerPassword, localSave = true) {
      this.managerPassword = managerPassword
      cache.managerPassword.set(managerPassword)
      if (localSave) await zipPlugin.setLocalItemPreload('managerPassword', managerPassword)
    },
    initLocalData({ serverPath, managerName, managerPassword }) {
      this.setServerPath(serverPath, false)
      this.setManagerName(managerName, false)
      this.setManagerPassword(managerPassword, false)
    }
  }
})
